/**   
 * @Title: JfinalStaticUtil.java
 * @Package com.ewin.util
 * @Description: 
 * @author wangxiao
 * @date 2017-4-8 下午6:46:22
 */
package com.wx.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import com.jfinal.kit.HashKit;
import com.xiaoleilu.hutool.util.StrUtil;

/**   
 * @ClassName: JfinalStaticUtil
 * @Description: 
 * @author wangxiao
 * @date 2017-4-8 下午6:46:22
 */
public class JfinalStaticUtil {
	/**
	 * 
	 * @Title: checkStrExis
	 * @Description: 查找字符串是否存在
	 * @param checkStr
	 * @param findStr
	 * @return
	 */
	public static String checkStrExis(ArrayList<ArrayList<String>> strArr,String findStr){
		String i ="";
		if(!StrUtil.isEmpty(findStr)){
			for(int j=0;j<strArr.size();j++){
				//循环解析
				if(findStr.indexOf(strArr.get(j).get(0))>=0){
					i=strArr.get(j).get(1);
					break;
				}
			}
		}
		return i;
	}
	/**
	 * 
	 * @Title: checkStrExis
	 * @Description: 查找字符串是否存在
	 * @param checkStr
	 * @param findStr
	 * @return
	 */
	public static String[] splitStr(String str,String splitstr){
		if(!StrUtil.isBlank(str)){
			
			String[] arr =  new String[2];
			if(!StrUtil.isBlank(str.substring(0, str.indexOf(splitstr)))){
				arr[0] = str.substring(0, str.indexOf(splitstr));
			}else{
				arr[0] = "";
			}
			if(!StrUtil.isBlank(str.substring( str.indexOf(splitstr)+1))){
				arr[1] = str.substring( str.indexOf(splitstr)+1);
			}else{
				arr[1] = "";
			}
			return arr;
		}
		return null;
	}
	/**
	 * 
	 * @Title: checkStrExis
	 * @Description: 查找字符串是否存在
	 * @param checkStr
	 * @param findStr
	 * @return
	 */
	public static String replaceStr(String str,String fs,String ts){
		return str.replace(fs, ts);
	}
	/**
	 * 
	 * @Title: getTime
	 * @Description: 获得日期 可根据参数获得相隔天数
	 * @param datetemp
	 * @param i
	 * @param formyType
	 * @return
	 */
	public static String getTime(String datetemp,int i,String formyType){
		SimpleDateFormat df = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss");
		String time="";
		Calendar cal = Calendar.getInstance(); 
		Date date;
        if(!StrUtil.isEmpty(datetemp)){
        	try {
    			date = df.parse(datetemp);
    		} catch (ParseException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    			date = new Date();
    		}
		}else{
			date = new Date();
		}
		
	    SimpleDateFormat sdf = new SimpleDateFormat(formyType);
	    try 
        { 
            cal.setTime(date); 
            cal.add(Calendar.DATE, i); 
            time = sdf.format(cal.getTime()); 
        } 
        catch (Exception e) 
        { 
            // TODO Auto-generated catch block 
            e.printStackTrace();
            time = sdf.format(new Date());
        }
	    return time;
	}
	
	/**
	 * 
	 * @Title: getTime
	 * @Description: 获得日期 可根据参数获得相隔天数
	 * @param datetemp
	 * @param i
	 * @param formyType
	 * @return
	 */
	public static long getTimeSjc(){
		return new Date().getTime();
	}
	
	/**
	 * 
	 * @Title: getSignKeyMd5
	 * @Description: 可转大小写的MD5加密方法
	 * @param sign
	 * @param type
	 * @return
	 */
	public static String getSignKeyMd5(String sign,int type){
		String md5="";
		if(type ==0){
			//小写
			md5 =HashKit.md5(sign).toLowerCase();
		}else{
			//大写
			md5 =HashKit.md5(sign).toUpperCase();
		}
		return md5;
	}
	
	/**
	 * 
	 * @Title: strToSpecStr
	 * @Description: 普通字符串转义成特殊字符串
	 * @param str
	 * @return
	 */
	public static double convertWeigh(String weight,int type,double coefficient){
		try{
			if(StrUtil.isEmpty(weight)){
				return 1.99;
			}
			if(type == 0){
				return Double.valueOf(weight)/coefficient;
			}else{
				return Double.valueOf(weight)*coefficient;
			}
			
		}catch (Exception e) {
			// TODO: handle exception
			return 1.99;
		}
	}
	/**
	 * 
	 * @Title: strToSpecStr
	 * @Description: 普通字符串转义成特殊字符串
	 * @param str
	 * @return
	 */
	public static double convertDshk(String weight,int type,double coefficient,int chosetype){
		Double dshk =0.00;
		try{
			if(StrUtil.isEmpty(weight)){
				dshk= 1.99;
				return dshk;
			}
			if(type == 0){
				dshk= Double.valueOf(weight)/coefficient;
			}else{
				dshk= Double.valueOf(weight)*coefficient;
			}
			if(chosetype == 0 ){
				//不取
			}else if(chosetype==1){
				//向上
				dshk= Math.ceil(dshk);
			}else if(chosetype==2){
				//向下
				dshk=Math.floor(dshk);
			}
		}catch (Exception e) {
			// TODO: handle exception
			return 1.99;
		}
		return dshk;
	}
	
	/**
	 * 
	 * @Title: getTime
	 * @Description: 获得日期 可根据参数获得相隔天数
	 * @param datetemp
	 * @param i
	 * @param formyType
	 * @return
	 */
	public static String convertTimeByType(String datetemp,String toType){
		
		SimpleDateFormat df = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss");
		Date date;
		String time="";
        if(!StrUtil.isEmpty(datetemp)){
        	try {
    			date = df.parse(datetemp);
    		} catch (ParseException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    			date = new Date();
    		}
		}else{
			date = new Date();
		}
		
	    SimpleDateFormat sdf = new SimpleDateFormat(toType);
	    try 
        { 
            time = sdf.format(date); 
        } 
        catch (Exception e) 
        { 
            // TODO Auto-generated catch block 
            e.printStackTrace();
            time = sdf.format(new Date());
        }
	    return time;
	}

	public static String getStrByFirstChar(String str,String start){
		return str.substring(str.indexOf(start)+1).trim();
	}
	public static String getStrByLastChar(String str,String start){
		return str.substring(str.lastIndexOf(start)+1).trim();
	}
	
	
}